/**********************************************************************

  Audacity: A Digital Audio Editor

  @file VST3OptionsDialog.cpp

  @brief Part of Audacity VST3 module, duplicate from VSTEffect.cpp

**********************************************************************/

#include "VST3OptionsDialog.h"
#include "ShuttleGui.h"
#include "ConfigInterface.h"
#include "widgets/valnum.h"

BEGIN_EVENT_TABLE(VST3OptionsDialog, wxDialogWrapper)
EVT_BUTTON(wxID_OK, VST3OptionsDialog::OnOk)
END_EVENT_TABLE()

VST3OptionsDialog::VST3OptionsDialog(const EffectDefinitionInterface& effect)
    : wxDialogWrapper{nullptr, wxID_ANY, XO("VST Effect Options")}
    , mEffect{effect}
{
    GetConfig(mEffect, PluginSettings::Shared, wxT("Options"),
              wxT("BufferSize"), mBufferSize, 8192);
    GetConfig(mEffect, PluginSettings::Shared, wxT("Options"),
              wxT("UseLatency"), mUseLatency, true);
    GetConfig(mEffect, PluginSettings::Shared, wxT("Options"),
              wxT("UseGUI"), mUseGUI, true);

    ShuttleGui S(this, eIsCreating);
    PopulateOrExchange(S);
}

VST3OptionsDialog::~VST3OptionsDialog()
{
}

void VST3OptionsDialog::PopulateOrExchange(ShuttleGui& S)
{
    S.SetBorder(5);
    S.StartHorizontalLay(wxEXPAND, 1);
    {
        S.StartVerticalLay(false);
        {
            S.StartStatic(XO("Buffer Size"));
            {
                S.AddVariableText(XO(
                                      "The buffer size controls the number of samples sent to the effect "
                                      "on each iteration. Smaller values will cause slower processing and "
                                      "some effects require 8192 samples or less to work properly. However "
                                      "most effects can accept large buffers and using them will greatly "
                                      "reduce processing time."),
                                  false, 0, 650);

                S.StartHorizontalLay(wxALIGN_LEFT);
                {
                    wxTextCtrl* t;
                    t = S.Validator<IntegerValidator<int> >(
                        &mBufferSize, NumValidatorStyle::DEFAULT, 8, 1048576 * 1)
                        .MinSize({ 100, -1 })
                        .TieNumericTextBox(XXO("&Buffer Size (8 to 1048576 samples):"),
                                           mBufferSize,
                                           12);
                }
                S.EndHorizontalLay();
            }
            S.EndStatic();

            S.StartStatic(XO("Latency Compensation"));
            {
                S.AddVariableText(XO(
                                      "As part of their processing, some VST3 effects must delay returning "
                                      "audio to Audacity. When not compensating for this delay, you will "
                                      "notice that small silences have been inserted into the audio. "
                                      "Enabling this option will provide that compensation, but it may "
                                      "not work for all VST3 effects."),
                                  false, 0, 650);

                S.StartHorizontalLay(wxALIGN_LEFT);
                {
                    S.TieCheckBox(XXO("Enable &compensation"),
                                  mUseLatency);
                }
                S.EndHorizontalLay();
            }
            S.EndStatic();

            S.StartStatic(XO("Graphical Mode"));
            {
                S.AddVariableText(XO(
                                      "Most VST3 effects have a graphical interface for setting parameter values."
                                      " A basic text-only method is also available. "
                                      " Reopen the effect for this to take effect."),
                                  false, 0, 650);
                S.TieCheckBox(XXO("Enable &graphical interface"),
                              mUseGUI);
            }
            S.EndStatic();
        }
        S.EndVerticalLay();
    }
    S.EndHorizontalLay();

    S.AddStandardButtons();

    Layout();
    Fit();
    Center();
}

void VST3OptionsDialog::OnOk(wxCommandEvent& WXUNUSED(evt))
{
    if (!Validate()) {
        return;
    }

    ShuttleGui S(this, eIsGettingFromDialog);
    PopulateOrExchange(S);

    SetConfig(mEffect, PluginSettings::Shared, wxT("Options"),
              wxT("BufferSize"), mBufferSize);
    SetConfig(mEffect, PluginSettings::Shared, wxT("Options"),
              wxT("UseLatency"), mUseLatency);
    SetConfig(mEffect, PluginSettings::Shared, wxT("Options"),
              wxT("UseGUI"), mUseGUI);

    EndModal(wxID_OK);
}
